package com.w.wx.controller;


import com.w.wx.domain.Msg;
import com.w.wx.domain.Notice;
import com.w.wx.domain.UserEr;
import com.w.wx.service.impls.GoodServiceSImpl;
import com.w.wx.service.impls.ImagesServiceImpl;
import com.w.wx.service.impls.UserServiceImpl;
import com.w.wx.utils.ALToHMUtil;
import com.wf.captcha.utils.CaptchaUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Iterator;

@Slf4j
@RestController
@RequestMapping("/wx/admin")
public class AdminContro {

    @Autowired
    private GoodServiceSImpl goodService;
    @Autowired
    private ImagesServiceImpl imagesService;
    @Autowired
    private UserServiceImpl userService;

    /**
     * 审核接口，修改物品状态，并设置审核通过时间
     * flag==1为通过，flag==2为审核未通过
     * @return
     */
    @RequestMapping("/setState")
    public Msg setGoodsState(Integer gid,Integer state, String reason){
        return ALToHMUtil.retrunEasy(goodService.setGoodsState(gid,state,reason));
    }

    /**
     * 删除不爽的物品
     * @param gid
     * @return
     */
    @GetMapping("/delete")
    public Msg deleteGoods(Integer gid){
        if (gid==null){
            return Msg.fail().add("error","参数为空");
        }
        return ALToHMUtil.retrunEasy(goodService.deleteGoods(gid));
    }


    /**
     * 获取所有公告
     * @return
     */
    @RequestMapping("/getNotice")
    public Msg getNotice(){
        ArrayList<Notice> notice = imagesService.getAllNoticeInfo();

        return ALToHMUtil.listTomapNotice(notice);
    }


    /**
     * 更新公告
     * @param mid
     * @return
     */
    @RequestMapping("/updateNotice")
    public Msg updateNotice(Integer mid , String noticeInfo){
        return ALToHMUtil.retrunEasy(imagesService.updateNotice(mid,noticeInfo));
    }

    /**
     * 获取所有用户封面
     * @return
     */
    @RequestMapping("/getAllUserSimInfo")
    public Msg getAllUserSimInfo(){
        return ALToHMUtil.listTomapUserEr(userService.getAllUserSimInfo());
    }

    /**
     * 获取用户详细详细和认证图片
     * @param openid
     * @return
     */
    @RequestMapping("getDeUserAndImage")
    public Msg getErUserAndImage(String openid){
        if (openid==null){
            return Msg.fail().add("error","参数为空");
        }
        UserEr uimage = userService.getErUserAndImage(openid);
        if(uimage!=null){
            Msg msg =new Msg();
            msg.setMsg("处理成功");
            msg.setCode(200);
            msg.add("uimage",uimage);
            return msg;
        }else{
            return Msg.fail();
        }
    }

    /**
     * 验证码
     * @param request
     * @param response
     * @throws Exception
     */
    @RequestMapping("/captcha")
    public void captcha(HttpServletRequest request, HttpServletResponse response) throws Exception {
        CaptchaUtil.out(request, response);

    }

    /**
     * 管理员登陆
     * @param passwd
     * @param username
     * @return
     */
    @PostMapping("/adminLogin")
    public Msg adminLogin(HttpServletRequest request,String passwd , String username,String verCode){
        String pw = "Admin2021";
        String uname = "admin";
      //  if (!CaptchaUtil.ver(verCode, request)) {
//            CaptchaUtil.clear(request);  // 清除session中的验证码
//            return Msg.fail().add("error","验证码不正确！！");
       //}
        if (pw.equals(passwd)&&uname.equals(username)){
            return Msg.success();
        }else {
            return Msg.fail().add("error","账号或者密码不正确！！");
        }

    }

    /**
     * state == 1 审核通过; state == 2 审核不通过; state == 3 待审核; state == 10 拉黑; state == 0无操作
     * @param openid
     * @param state
     * @param reason
     * @return
     */
    @RequestMapping("userCheck")
    public Msg setUserCheckState(String openid,Integer state, String reason){
        return userService.setUserCheckState(openid,state,reason);
    }

    /**
     * 获取用户审核不通过或者拉黑原因
     * @param openid
     * @return
     */
    @RequestMapping("userIsOk")
    public Msg userCheckIsOk(String openid){
        return userService.userCheckIsOk(openid);
    }


    /**
     * 判断是否暴露管理员入口
     */
    @RequestMapping("isAdmin")
    public Msg isAdmin(String openid){
        ArrayList<String> list = new ArrayList<>();
        list.add("oApmq5GpnR0o0GHN9jrO_AXk5VsM");
        list.add("oApmq5Cb-_U6qpr17bWBp3RMikrs");
        list.add("oApmq5Mdz3taOKdfslh3il53m_1s");
        Iterator<String> it =list.iterator();
        while(it.hasNext()){
            if (openid.equals(it.next())){
                return Msg.success().add("tip","此用户是管理员");
            }
        }
        return Msg.fail().add("tip","此用户非管理员");
    }
}
